#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>

#define KEY_CNT			1		// 设备号个数
#define KEY_NAME		"key"	// 名字
#define KEY0VALUE		0xF0
#define INVAKEY			0x00

/* key设备结构体 */
struct key_dev{
	dev_t devid;			 // 设备号
	int major;				 // 主设备号
	int minor;				 // 次设备号
	struct cdev cdev;		 // cdev
	struct class * class;	 // 类
	struct device * device;	 // 设备
	struct device_node * nd; // 设备节点
	int key_gpio;			 // key所使用的GPIO编号
	atomic_t keyvalue;
};

struct key_dev key;			 // key设备

static int key_open(struct inode *inode, struct file *filp)
{
	filp->private_data = &key; 	// 设置私有数据

	return 0;
}

static int key_release(struct inode *inode, struct file *filp)
{

	return 0;
}

static ssize_t key_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
	int ret = 0;

	return ret;
}

static ssize_t key_read(struct file * filp, char __user * buf, size_t count, loff_t * ppos)
{
	int value;
	struct key_dev * dev = filp->private_data;
	int ret = 0;

	if (gpio_get_value(dev->key_gpio) == 0) {	/* 按下 */
		while (!gpio_get_value(dev->key_gpio));
		atomic_set(&dev->keyvalue, KEY0VALUE);
	}
	else {
		atomic_set(&dev->keyvalue, INVAKEY);
	}

	value = atomic_read(&dev->keyvalue);
	ret = copy_to_user(buf, &value, sizeof(value));

	return ret;
}

/* 操作集 */
static const struct file_operations key_fops = {
	.owner   = THIS_MODULE,
	.write   = key_write,
	.read    = key_read,
	.open    = key_open,
	.release = key_release,
};

/* key io初始化 */
static int keyio_init(struct key_dev * dev)
{
	int ret = 0;
	dev->nd = of_find_node_by_path("/key");
	if (dev->nd == NULL) {
		ret = -EINVAL;
		goto fail_nd;
	}

	dev->key_gpio = of_get_named_gpio(dev->nd, "key-gpio", 0);
	if (dev->key_gpio < 0) {
		ret = -EINVAL;
		goto fail_gpio;
	}

	ret = gpio_request(dev->key_gpio, "key0");
	if (ret) {
		ret = -EBUSY;
		printk("IO %d cant't request!\r\n", dev->key_gpio);
		goto fail_request;
	}

	ret = gpio_direction_input(dev->key_gpio);
	if (ret < 0) {
		ret = -EINVAL;
		goto fail_input;
	}

	return 0;

	fail_input:		gpio_free(dev->key_gpio);
	fail_request:
	fail_gpio:
	fail_nd:		return ret;
}

/* 驱动入口函数 */
static int __init key_init(void)
{
	int ret = 0;

	/* 初始化atomic */
	atomic_set(&key.keyvalue, INVAKEY);

	/* 获取设备号 */
	key.major = 0;
	if (key.major) {	//  定义了设备号
		key.devid = MKDEV(key.major, 0);
		register_chrdev_region(key.devid, KEY_CNT, KEY_NAME);
	} 
	else {				// 没有定义设备号
		alloc_chrdev_region(&key.devid, 0, KEY_CNT, KEY_NAME);	// 申请设备号
		key.major = MAJOR(key.devid);	// 获取分配号的主设备号
		key.minor = MINOR(key.devid);	// 获取分配号的次设备号
	}
	if (ret < 0) {
		goto fail_devid;
	}

	printk("key major = %d, min = %d\r\n", key.major, key.minor);
	
	/* 初始化cdev */
	key.cdev.owner = THIS_MODULE;
	cdev_init(&key.cdev, &key_fops);
	
	/* 添加一个cdev */
	ret = cdev_add(&key.cdev, key.devid, KEY_CNT);
	if (ret) {
		goto fail_cdevadd;
	}

	/* 创建类 */
	key.class = class_create(THIS_MODULE, KEY_NAME);
	if (IS_ERR(key.class)) {
		ret = PTR_ERR(key.class);
		goto fail_class;
	}

	/* 创建设备 */
	key.device = device_create(key.class, NULL, key.devid, NULL, KEY_NAME);
	if (IS_ERR(key.device)) {
		ret = PTR_ERR(key.device);
		goto fail_device;
	}

	ret = keyio_init(&key);
	if (ret < 0) {
		goto fail_ioinit;
	}
	
	return 0;

	fail_ioinit:	device_destroy(key.class, key.devid);
	fail_device:	class_destroy(key.class);
	fail_class:		cdev_del(&key.cdev);
	fail_cdevadd:	unregister_chrdev_region(key.devid, KEY_CNT);
	fail_devid:		return ret;
}

static void __exit key_exit(void)
{
	gpio_free(key.key_gpio);
	device_destroy(key.class, key.devid);
	class_destroy(key.class);
	cdev_del(&key.cdev);
	unregister_chrdev_region(key.devid, KEY_CNT);
}

module_init(key_init);
module_exit(key_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("huan");
